package com.heima.netty.async;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

/**
 * Netty Future
 * @author ZhengDp
 * @Date 2023/6/24 22:07
 */
@Slf4j
public class NettyFutureTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup(1);
        // 每个 EventLoop 都只有一个线程
        EventLoop eventLoop = group.next();
        Future<Integer> future = eventLoop.submit(() -> {
            try {
                log.info("Calculate ... ");
                Thread.sleep(1000);
                return 70;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        //log.info("Waiting Result .... ");
        //log.info("Result : {}" , future.get());
        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.info("Get Result : {}",future.getNow());
            }
        });
    }
}
